<template>
  <div class="wrapper">
    <JForm :form-config="dynamicValidateForm" @handleBoxEvent="handleBoxEvent" />
  </div>
</template>

<script>
import JForm from '@/components/JForm'
import { getDictListByType } from '@/api/dictionaries'
import { getTenant } from '@/api/tenant'
import { getOrgTree } from '@/api/org'

export default {
  name: 'AreaUpdate',
  components: { JForm },
  props: {
    emitData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    const userInfo = [
      {
        name: 'name',
        label: '名称',
        type: 'string',
        required: true
      },
      {
        name: 'code',
        label: '编码',
        type: 'string',
        required: true
      },
      {
        name: 'tenantCode',
        label: '所属租户',
        required: true,
        type: 'option',
        disabled: true,
        config: {
        }
      },
      {
        name: 'parentOrg',
        label: '上级部分',
        type: 'select',
        required: true,
        placeholder: '地址',
        config: {
          props: { label: 'title', value: 'id' }
        }
      },
      {
        name: 'contactName',
        label: '联系人姓名',
        type: 'string',
        required: false,
        placeholder: '联系人姓名'
      },
      {
        name: 'contactEmail',
        label: '联系人邮箱',
        type: 'string',
        required: false,
        placeholder: '联系人邮箱'
      },
      {
        name: 'contactEmail',
        label: '联系人电话',
        type: 'string',
        required: false,
        placeholder: '联系人电话'
      },
      {
        name: 'headName',
        label: '领导人名字',
        type: 'string',
        required: false,
        placeholder: '领导人名字'
      },
      {
        name: 'headEmail',
        label: '领导人邮箱',
        type: 'string',
        required: false,
        placeholder: '领导人邮箱'
      },
      {
        name: 'headPhone',
        label: '领导人电话',
        type: 'string',
        required: false,
        placeholder: '领导人电话'
      },
      {
        name: 'isVirtual',
        label: '虚拟机构',
        type: 'option',
        required: false,
        value: '0',
        config: {
        }
      },
      {
        name: 'sort',
        label: '排序',
        type: 'string',
        required: false
      },
      {
        name: 'remark',
        label: '备注说明',
        type: 'string',
        required: false
      },
      {
        name: 'address',
        label: '地址',
        type: 'string',
        required: true,
        placeholder: '地址'
      }
    ]
    return {
      userInfo,
      formData: {},
      CITY_TYPE: [],
      tenantList: [],
      parentOrg: []
    }
  },
  computed: {
    clientId() {
      const { id = null } = this.client
      return id
    },
    dynamicValidateForm() {
      const data = this.userInfo
      const result = []
      data.forEach(i => {
        const { type, name } = i
        if (type === 'option') {
          i.config.options = this.filterOption(i)
        }
        if (type === 'select') {
          i.config.options = this[name]
        }
        result.push(i)
      })
      if (this.formData) {
        result.forEach(i => {
          const { name } = i
          i.value = this.formData[name]
        })
      }
      return result
    },
    isVirtualOption() {
      const data = this.CITY_TYPE
      data.forEach(item => {
        const { id, name } = item
        item.value = '' + id
        item.label = name
      })
      return data
    },
    tenantCodeOption() {
      const data = this.tenantList
      data.forEach(item => {
        const { tenant_code, tenant_name } = item
        item.value = tenant_code
        item.label = tenant_name
      })
      return data
    }
  },
  watch: {
    emitData: {
      handler: function(newval, oldval) {
        const isEmpty = Object.keys(newval)
        if (isEmpty.length) {
          this.formData = newval
        }
      },
      immediate: true
    }
  },
  created() {
    this.getDicCITY_TYPE()
    this.getTenantList()
    this.getOrgTree()
  },
  mounted() {},
  methods: {
    getDicCITY_TYPE() {
      getDictListByType({ dictTypeCode: 'YN01' }).then(res => {
        const { code, data /* message */ } = res
        if (code === 200) { this.CITY_TYPE = data }
      })
    },
    handleBoxEvent(emitObj) {
      this.submitForm(emitObj)
    },
    filterOption(item) {
      const key = item.name
      return this[`${key}Option`]
    },
    submitForm(emitObj) {
      // 父级ID 祖籍id 租户编码 id
      this.$emit('handleBoxEvent', { target: emitObj })
    },
    // 获取部门
    getOrgTree() {
      getOrgTree({ tenantCode: '000000' }).then(res => {
        const { data, code } = res
        if (code === 200) {
          this.parentOrg = data
        }
      })
    },
    getTenantList() {
      getTenant().then(res => {
        const { code, data } = res
        if (code === 200) {
          this.tenantList = data
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.wrapper{
   padding: 0px;
   margin: 0px;
}
</style>
